package site.suremotoo.demo.delaypay.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import site.suremotoo.demo.delaypay.consumer.MqConsumer;
import site.suremotoo.demo.delaypay.producer.MqProducer;

import java.io.IOException;

/**
 * @author liulx
 * @description TODO
 * @date 2020-12-28 21:04
 */
@Controller
public class TestMqController {

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private MqConsumer mqConsumer;

    @RequestMapping("order")
    public String sendOrderMsg(@RequestParam String orderId, Model model) {
        boolean isOk = mqProducer.sendOrderMsg(orderId);
        if (!isOk) {
            return "failorder";
        }
        mqProducer.sendPayMsg(orderId);
        model.addAttribute("orderId", orderId);
        return "order";
    }

    @RequestMapping("pay")
    public String sendPayMsg(@RequestParam String orderId, Model model) throws IOException {
        mqConsumer.payConsumer();
        System.out.println("订单：" + orderId + " 支付成功!");
        model.addAttribute("orderId", orderId);
        return "success";
    }

    @RequestMapping("/failorder")
    public String failOrder(@RequestParam String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        return "fail";
    }

}
